//
//  HDCFRegisterVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/3.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFFindPsw.h"
#import "YHVerifyPhoneNum.h"
#import "JKCountDownButton.h"
#import "SVProgressHUD.h"

#define KRegisterUrl @"Auth/Register"

@interface HDCFFindPsw ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet JKCountDownButton *codeBtnClick;

@end

@implementation HDCFFindPsw

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer*tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(registFirstRespond:)];
    [self.view addGestureRecognizer:tap];
    
    self.phoneTF.keyboardType=UIKeyboardTypeNumberPad;
    self.codeTF.keyboardType=UIKeyboardTypeNumberPad;
   [self.navigationItem setTitle:@"找回密码"];
}

-(void)registFirstRespond:(UIGestureRecognizer*)tap
{
    [self.view endEditing:YES];
}


- (IBAction)codeBtnClick:(JKCountDownButton *)sender {
    
    if (self.phoneTF.text.length < 1) {
         [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    
   [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"Phone"] =self.phoneTF.text;
    paramets[@"type"] = @"0";
    paramets[@"CheckExistentOrNo"] = @"true";
    NSString *jsonStr = [self dictionaryToJson:paramets];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/Auth/SendPwdResetCode" WithParamets:jsonStr WithParamet:nil success:^(id result) {
        NSDictionary*dic=(NSDictionary*)result;
        NSNumber*number=[dic objectForKey:@"code"];
        if ([number isEqual:@1]) {
            [SVProgressHUD showSuccessWithStatus:@"发送成功"];
            [sender startWithSecond:60];
            [SVProgressHUD dismissWithDelay:1.0];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    return jsonTemp;
}


- (IBAction)registerBtnClick:(UIButton *)sender {
    
    if (self.phoneTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    
    if (self.codeTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    
    if (self.passwordTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"Phone"] =self.phoneTF.text;
    paramets[@"CheckCode"] = self.codeTF.text;
     paramets[@"NewPwd"] = self.passwordTF.text;
    
    NSString *jsonStr = [self dictionaryToJson:paramets];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/Auth/ModifyPwd" WithParamets:jsonStr WithParamet:nil success:^(id result) {
        
        NSDictionary*dic=(NSDictionary*)result;
        NSNumber*number=[dic objectForKey:@"code"];
        if ([number isEqual:@1]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];

    
}

//延迟几秒调用
-(void)addtimerWith:(double)time
{
    
    [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timerpush) userInfo:nil repeats:NO];
    
}

-(void)timerpush
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
